



var FILTER_CTRL = "Crm:FilterControl";
var FILTERFIELDGRP_CTRL = "Crm:FilterFieldGroup";
var FILTERFIELD_CTRL = "Crm:FilterField";
var FIELDLIST_CTRL = "Crm:FieldList";
var OPERATORLIST_CTRL = "Crm:OperatorList";
var CLIENTCONTROl_CTRL = "Crm:ClientControl";
var AUTOSHOW_CTRL = "Crm:AutoShow";
var FILTERENTITY_CTRL = "Crm:FilterEntity";
var QUERYLIST_CTRL = "Crm:QueryList";

var XPATH_SHOWNINSIMPLE = ".//condition[(not(@uihidden) or @uihidden='0')]";

var XPATH_EMPTYFILTER = ".//filter[not(.//condition)]";

var FIELD_GROUP = "fld";
var ENTITY_GROUP = "ent";

var _oFetchXml = CreateXmlDocument(false);
var _oClientCache = null;
var _AdvFindButtonsEnum = null;
var _AdvFindModesEnum = null;
var _FetchModesEnum = null;

var _sAliasForLinkEntity;

function ConvertLikeToUserType(s)
{


s = s.replace(/\[%\]/g, "[*]");
s = s.replace(/%/g, "*");

s = s.replace(/\[\*\]/g, "%");
s = s.replace(/\[\[\]/g, "[");
return s.replace(/\[_\]/g, "_");
}


function GetFetchAttributes(sFetchXml)
{
_oFetchXml.loadXML(sFetchXml);
var sAttribs		= "";
var oAttribNodes	= _oFetchXml.selectNodes('/fetch/entity/attribute');
var nLength			= oAttribNodes.length;


for (var i = 0; i < nLength; i++)
{
sAttribs += oAttribNodes.nextNode().xml;
}

if(Trim(sAttribs)=="")
{
sAttribs = "<all-attributes />";
}


var oOrderNode	= _oFetchXml.selectSingleNode('/fetch/entity/order');
if (!IsNull(oOrderNode))
{
sAttribs += oOrderNode.xml;
}
return sAttribs;
}


function ShowIfDetailedOrNotHidden(oCtrl, bNotHidden, iMode)
{
oCtrl.style.display = (bNotHidden||iMode==oCtrl.Container.ModesEnum.Detailed) ? "inline" : "none";
}

function IsDefaultAdvancedFindView(sQueryId, sEntityName, oClientCache)
{
var oQryData = oClientCache.GetElement("DefaultAdvFindView", sEntityName);
if(!IsNull(oQryData))
{
return oQryData.QueryId == sQueryId;
}
return false;
}

function ConfigureToolbars(oAdvFind)
{
var oToolbar;
var sOp = "attachclick";

with(oAdvFind)
{

oToolbar = all.mnuBar7;
if(!IsNull(oToolbar))
{
oToolbar = oToolbar.rows[0].cells[0];

PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnSave, id+".Save()");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnSaveAs, id+".SaveAs()");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnNew, id+".New()");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnEditView, id+".EditView()");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnEditProperties, id+".EditProperties()");
}

if(Modes&ModesEnum.Detailed)
{

oToolbar = all.mnuBar5.rows[0].cells[0];

PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnSimple, id+".Mode="+ModesEnum.Simple+";"+id+".ConfigureMode()");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnAnd, id+".Group('and')");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnOr, id+".Group('or')");
PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnClear, id+".ClearFromToolbar()");
}


if(!IsNull(all("mnuBar6"))&&(Modes&ModesEnum.Simple))
{

oToolbar = all.mnuBar6.rows[0].cells[0];

PerformToolbarOperation(sOp, oToolbar, oToolbar.all.btnDetailed, id+".Mode="+ModesEnum.Detailed+";"+id+".ConfigureMode()");
}
}
}
function generateNextAlias(sAlias)
{
var c1 = sAlias.charCodeAt(0);
var c2 = sAlias.charCodeAt(1);

c2++;

if(c2 > 122)
{
c2 = 97;
c1++;
if(c1 > 122)
{
return null;
}
}
return String.fromCharCode(c1, c2);
}

function GetNextAliasForLinkEntity(sLayoutXml, sAliasesInInitialFetch)
{

if(IsNull(_sAliasForLinkEntity) || _sAliasForLinkEntity.length == 0)
{
_sAliasForLinkEntity = "aa";
}
else
{
_sAliasForLinkEntity = generateNextAlias(_sAliasForLinkEntity);
}


var regEx = new RegExp("name\\s*=\\s*['|\"]" + _sAliasForLinkEntity + "\.", "igm");
while(regEx.test(sLayoutXml) || (sAliasesInInitialFetch.indexOf("|"+_sAliasForLinkEntity+"|")!=-1))
{
_sAliasForLinkEntity = generateNextAlias(_sAliasForLinkEntity);
regEx.source = "name\\s*=\\s*['|\"]" + _sAliasForLinkEntity + "\.";
}

return _sAliasForLinkEntity;
}
